﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using FANN.Net;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using System.IO;
using Emgu.CV;
using CascadeClassifier = OpenCvSharp.CPlusPlus.CascadeClassifier;
using HOGDescriptor = OpenCvSharp.CPlusPlus.HOGDescriptor;
using ThreadState = System.Threading.ThreadState;
using Emgu.CV.Util;
using Mat = OpenCvSharp.CPlusPlus.Mat;


namespace Сlassifier_Tool_For_OpenCV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            foreach (var mkey in Enum.GetNames(typeof(ActivationFunction)))
            {

                comboBox4.Items.Add(mkey);
                comboBox5.Items.Add(mkey);
            }

            comboBox5.SelectedIndex = 3;
            comboBox4.SelectedIndex = 4;
            foreach (var mkey in Enum.GetNames(typeof(TrainingAlgorithm)))
            {
                comboBox6.Items.Add(mkey);
            }
            comboBox6.SelectedIndex = 0;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_worker != null && _worker.ThreadState == ThreadState.Running)
                _worker.Abort();
            ann.StopSimple();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            panel1.Enabled = !checkBox2.Checked;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                if (Directory.Exists(textBox2.Text))
                {
                    SetSetting();

                    log.Text += "\r\n Start create files HaarTraining\r\n";
                    log.Refresh();
                    progressBar1.Value = 0;
                    var posFile = Directory.GetFiles(textBox1.Text).ToList();
                    var negFile = Directory.GetFiles(textBox2.Text).ToList();
                    if (checkBox1.Checked)
                    {
                        posFile.AddRange(Directory.GetDirectories(textBox1.Text).SelectMany(Directory.GetFiles));
                        negFile.AddRange(Directory.GetDirectories(textBox2.Text).SelectMany(Directory.GetFiles));
                    }
                    if (_worker != null && _worker.ThreadState == ThreadState.Running)
                        _worker.Abort();
                    _worker = new Thread(() => CreateSamplesHaar(posFile, negFile));
                    _worker.Start();
                    textBox32.Text = "c:\\temp\\haar\\vec.vec";
                    textBox33.Text = "c:\\temp\\haar\\bg.dat";

                    textBox34.Text = _n;
                    textBox35.Text = negFile.Count.ToString(CultureInfo.InvariantCulture);

                }
                else
                {
                    log.Text += "Error! directory " + textBox2.Text + " not found.\r\n";
                }
            }
            else
            {
                log.Text += "Error! directory " + textBox1.Text + " not found.\r\n";
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox32.Text) && File.Exists(textBox33.Text))
            {
                int pcount;
                int ncount;

                if (int.TryParse(textBox34.Text, out pcount) && int.TryParse(textBox35.Text, out ncount))
                {

                    var psi =
                        new ProcessStartInfo(Application.StartupPath + "\\haartraining.exe",
                            "-vec \"" + textBox32.Text + "\" -bg \"" + textBox33.Text + "\" -npos " +
                            textBox34.Text + " -nneg " + textBox35.Text + " -w " + _w + " -h " + _h + " -mem 2000")
                        {
                            WorkingDirectory = Application.StartupPath,
                            UseShellExecute = false
                        };


                    log.Text += "Start Haar training\r\n";
                    var process = Process.Start(psi);
                    while (!process.HasExited) Thread.Sleep(1000);
                    log.Text += " Haar training complite\r\n";
                }
                else
                {
                    log.Text += "Error! \"Count pos \" or  \"Neg count \" is not int or == 0 \r\n";
                }

            }
            else
            {
                log.Text += "Error! Vec or bg file is not exists\r\n";
            }


        }

        private void button10_Click_1(object sender, EventArgs e)
        {
            var f = new Test2();
            f.Show();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Visible = tabControl1.SelectedIndex != 2 && tabControl1.SelectedIndex != 3 &&
                                tabControl1.SelectedIndex != 4;


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = _examples[_selS[listBox1.SelectedIndex]];
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            pictureBox1.Image = bitmap(_examples[_selS[listBox1.SelectedIndex]]);

        }

        #region Callback
        private delegate void ShowSaveNewCalbak(List<Item> items);

        private delegate void SetNewCal(int step, double errorOnData, double errorOnValidate);


        private void SetNew(int step, double errorOnData, double errorOnValidate)
        {
            if (chart10.InvokeRequired && chart10.InvokeRequired &&
                log.InvokeRequired && log.InvokeRequired &&
                progressBar3.InvokeRequired && progressBar3.InvokeRequired)
            {
                var d = new SetNewCal(SetNew);
                Invoke(d, new object[]
                {
                    step, errorOnData,errorOnValidate
                
                });
            }
            else
            {
                if (step == 0)
                {
                    chart10.Series[0].Points.Clear();
                    chart10.Series[1].Points.Clear();
                }
                log.Text += errorOnData.ToString("0.000000") + " ";
                chart10.Series[0].Points.AddXY(step, errorOnData);
                chart10.Series[1].Points.AddXY(step, errorOnValidate);
                progressBar3.Value = Convert.ToInt32(step / Convert.ToDouble(textBox58.Text) * 100);

            }

        }

        private void SetStep(int step, double errorOnData, double errorOnValidate)
        {
            if (chart12.InvokeRequired && chart12.InvokeRequired &&
                log.InvokeRequired && log.InvokeRequired &&
                progressBar3.InvokeRequired && progressBar3.InvokeRequired)
            {
                var d = new SetNewCal(SetStep);
                Invoke(d, new object[]
                {
                    step, errorOnData,errorOnValidate
                
                });
            }
            else
            {
                if (step == 0)
                {
                    chart12.Series[0].Points.Clear();
                    chart12.Series[1].Points.Clear();
                }

                chart12.Series[0].Points.AddXY(step, errorOnData);
                chart12.Series[1].Points.AddXY(step, errorOnValidate);


            }

        }
        private void ShowSaveNew(List<Item> items)
        {
            if (InvokeRequired && InvokeRequired)
            {
                var d = new ShowSaveNewCalbak(ShowSaveNew);
                Invoke(d, new object[] { items });
            }
            else
            {
                var f = new Form2(items, textBox57, textBox65.Text);
                f.Show();

            }

        }

        private delegate void SetNetCalbak();
        private delegate void SetStepCalback(double dat, double t, double index);
        private delegate void SetCallback(double v, bool clear);
        private delegate void SetCallbackSingl(double v);
        private delegate void ShowSaveCalbak();
        private delegate void SetTextCallback(string value);

        private delegate void ProgressCallback(int value);

        private delegate void ProgressVisibleCallback(bool value);

        private void Progress(int value)
        {
            if (progressBar1.InvokeRequired && progressBar1.InvokeRequired)
            {
                var d = new ProgressCallback(Progress);
                Invoke(d, new object[] { value });
            }
            else
            {

                progressBar1.Visible = Visible;
                progressBar1.Value = value;
                progressBar1.Refresh();
            }
        }

        private void ProgressVisible(bool value)
        {
            if (progressBar1.InvokeRequired && progressBar1.InvokeRequired)
            {
                var d = new ProgressVisibleCallback(ProgressVisible);
                Invoke(d, new object[] { value });
            }
            else
            {

                progressBar1.Visible = value;
                progressBar1.Refresh();

            }
        }

        private void SetText(string value)
        {
            if (log.InvokeRequired && log.InvokeRequired)
            {
                var d = new SetTextCallback(SetText);
                Invoke(d, new object[] { value });
            }
            else
            {

                log.Text += value;
                log.Refresh();

            }
        }

        #endregion

        #region HOG

        private CvSize _winSize;
        private CvSize _blockSize;
        private CvSize _blockStride;
        private CvSize _cellSize;
        private int _nbins;
        private int _aperature;
        private int _winSigma;
        private string _w;
        private string _h;
        private string _a1;
        private string _a2;
        private string _a3;
        private string _n;
        private int _bg;


        private bool SetSetting()
        {
            try
            {
                _winSize = new CvSize(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text));
                _blockSize = new CvSize(Convert.ToInt32(textBox6.Text), Convert.ToInt32(textBox5.Text));
                _blockStride = new CvSize(Convert.ToInt32(textBox8.Text), Convert.ToInt32(textBox7.Text));
                _cellSize = new CvSize(Convert.ToInt32(textBox10.Text), Convert.ToInt32(textBox9.Text));
                _nbins = Convert.ToInt32(textBox11.Text);
                _aperature = Convert.ToInt32(textBox12.Text);
                _winSigma = Convert.ToInt32(textBox13.Text);

                _w = textBox16.Text;
                _h = textBox15.Text;
                _n = textBox17.Text;
                _a1 = textBox19.Text;
                _a2 = textBox18.Text;
                _a3 = textBox20.Text;

                if (checkBox2.Checked)
                    _hog = new HOGDescriptor();
                else
                    _hog = new HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins, _aperature,
                        _winSigma);
                return true;
            }
            catch
            {
                SetText("Error! Setting incorrect. All values ​​must be Integer\r\n");
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                textBox2.Text = folderBrowserDialog1.SelectedPath;
        }

        private HOGDescriptor _hog;

        private float[] Hog(string file)
        {
            var im = new IplImage(file, LoadMode.GrayScale);
            var image = new IplImage(_winSize, im.Depth, im.NChannels);
            Cv.Resize(im, image);
            var m = new Mat(image);
            var f = _hog.Compute(m);

            image.Dispose();
            m.Dispose();
            im.Dispose();
            return f;
        }


        private float[] vector(string file)
        {
            using (IplImage image = new IplImage(file, LoadMode.GrayScale))
            using (IplImage res = new IplImage(new CvSize(100, Convert.ToInt32(image.Height / (image.Width / 100.0))), BitDepth.U8, 1))
            {
                Cv.Resize(image, res, Interpolation.Area);
                List<float> ret = new List<float>();
                for (int i = 0; i < 100; i++)
                {
                    var c = res.GetCol(i);
                    ret.Add((float)c.Sum().Val0);
                    c.Dispose();
                }

                var rr = ret.Select(t => (t - ret.Min()) / (ret.Max() - ret.Min()));
                return rr.ToArray();
            }
        }


        private float[] brisk(string file)
        {
            Emgu.CV.Features2D.SIFTDetector ff = new Emgu.CV.Features2D.SIFTDetector(0, 3, 0.0, 10, 1);

            List<System.Drawing.PointF> k = new List<System.Drawing.PointF>();
            for (int x = _blockSize.Width / 2; x < _winSize.Width; x += _blockSize.Width)
                for (int y = _blockSize.Height / 2; y < _winSize.Height; y += _blockSize.Height)
                    k.Add(new System.Drawing.PointF(x, y)
                    {

                    });
            VectorOfKeyPoint v = new VectorOfKeyPoint();
            v.Push(k.Select(t => new Emgu.CV.Structure.MKeyPoint()
            {
                Point = t,
                Angle = 0,
                Size = 8,
                Response = 1,
                Octave = 1

            }).ToArray());
            Emgu.CV.Image<Emgu.CV.Structure.Gray, byte> im = new Image<Emgu.CV.Structure.Gray, byte>(file);
            var image = im.Resize(Convert.ToDouble(_winSize.Width) / im.Width, Emgu.CV.CvEnum.INTER.CV_INTER_AREA);
            var f = ff.ComputeDescriptorsRaw(image, null, v);
            im.Dispose();
            image.Dispose();
            List<float> r = new List<float>();
            for (int i = 0; i < f.Width; i++)
                for (int j = 0; j < f.Height; j++)
                    r.Add(f[j, i]);

            f.Dispose();


            return r.ToArray()


    ;
        }


        private float[] Hog(OpenCvSharp.IplImage im)
        {
            var image = new IplImage(_winSize, im.Depth, im.NChannels);
            Cv.Resize(im, image);
            var m = new Mat(image);
            var f = _hog.Compute(m);
            image.Dispose();
            m.Dispose();

            return f;
        }

        private string _hogfile;


        private void ComputeHOG(ICollection<string> posFile, ICollection<string> negFile, string output)
        {
            var lastrp = 0;
            if (_tstate)
                ProgressVisible(true);
            if (File.Exists(output))
                File.Delete(output);
            var full = posFile.Count + negFile.Count;
            var pos = 0;
            const int neg = 0;
            var sb = new StringBuilder();
            var c = 0;
            foreach (var file in posFile)
            {
                try
                {
                    var v = Hog(file);
                    var j = 1;
                    sb.Append("1");
                    foreach (var i in v)
                        sb.AppendFormat(" {0}:{1}", j++, i.ToString(CultureInfo.InvariantCulture));
                    pos++;
                    sb.AppendLine();
                    c++;

                }
                catch (Exception exception)
                {
                    SetText(file + "\r\n" + (exception.Message) + "\r\n");

                }

                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp & _tstate)
                {
                    lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                    Progress(lastrp);
                }


                if (c > 1000)
                {
                    File.AppendAllText(output, sb.ToString());
                    sb.Clear();
                    c = 0;

                }

            }

            File.AppendAllText(output, sb.ToString());
            sb.Clear();
            c = 0;

            foreach (var file in negFile)
            {
                try
                {
                    var v = Hog(file);
                    var j = 1;
                    sb.Append("-1");
                    foreach (var i in v)
                        sb.AppendFormat(" {0}:{1}", j++, i.ToString(CultureInfo.InvariantCulture));
                    pos++;
                    sb.AppendLine();
                    c++;

                }
                catch (Exception exception)
                {
                    SetText(file + "\r\n" + (exception.Message) + "\r\n");

                }
                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp & _tstate)
                {
                    lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                    Progress(lastrp);
                }

                if (c > 1000)
                {
                    File.AppendAllText(output, sb.ToString());
                    sb.Clear();
                    c = 0;

                }

            }
            File.AppendAllText(output, sb.ToString());
            if (_tstate)
                SetText(" Compute HogDescriptors copmlit. pos=" + posFile.Count + " neg=" +
                        negFile.Count + "\r\n");
            _hogfile = output;
            if (_tstate)
                SetText(" HogDescriptors file save to " + output + "\r\n");
            if (_tstate)
                ProgressVisible(false);
            _tstate = true;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (File.Exists(_svmfile))
            {
                openFileDialog2.InitialDirectory = Path.GetDirectoryName(_svmfile);
                openFileDialog2.FileName = Path.GetFileName(_svmfile);
            }
            if (openFileDialog2.ShowDialog() == DialogResult.OK && saveFileDialog3.ShowDialog() == DialogResult.OK)
            {
                var f = File.ReadAllLines(openFileDialog2.FileName);
                var i = Convert.ToInt32(f[7].Split(' ')[0]);
                var j = Convert.ToInt32(f[9].Split(' ')[0]) - 1;
                var a = Convert.ToDouble(f[10].Split(' ')[0], CultureInfo.InvariantCulture);
                var ar = new double[j, i];
                var al = new double[j];
                for (var index0 = 11; index0 < f.Length; index0++)
                {
                    var s = f[index0].TrimEnd('#');
                    var m = s.Split(' ');
                    al[index0 - 11] = Convert.ToDouble(m[0], CultureInfo.InvariantCulture);
                    for (var k = 1; k < m.Length - 1; k++)
                        ar[index0 - 11, Convert.ToInt32(m[k].Split(':')[0]) - 1] = Convert.ToDouble(m[k].Split(':')[1],
                            CultureInfo.InvariantCulture);

                }
                var d = new float[i + 1];
                var fff = "";
                for (var index0 = 0; index0 < i; index0++)
                {
                    d[index0] = 0;
                    for (var tt = 0; tt < j; tt++)
                        d[index0] += (float)(al[tt] * ar[tt, index0]);
                    fff += d[index0].ToString(CultureInfo.InvariantCulture) + " ";
                }

                d[i] = -(float)a;
                fff += (-a).ToString(CultureInfo.InvariantCulture);
                File.WriteAllText(saveFileDialog3.FileName, fff);
                log.Text += "Vector compute and save.\r\n";
            }

        }

        private bool _tstate;

        private void button6_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                if (Directory.Exists(textBox2.Text))
                {
                    if (SetSetting())
                    {
                        var tempfile1 = Path.GetRandomFileName();
                        log.Text += "\r\n Compute HOGDescriptor\r\n";
                        log.Refresh();
                        progressBar1.Value = 0;

                        var posFile = Directory.GetFiles(textBox1.Text).ToList();
                        var negFile = Directory.GetFiles(textBox2.Text).ToList();
                        if (checkBox1.Checked)
                        {
                            posFile.AddRange(Directory.GetDirectories(textBox1.Text).SelectMany(Directory.GetFiles));
                            negFile.AddRange(Directory.GetDirectories(textBox2.Text).SelectMany(Directory.GetFiles));
                        }
                        if (_worker != null && _worker.ThreadState == ThreadState.Running)
                            _worker.Abort();
                        _tstate = true;
                        ComputeHOG(posFile, negFile, Application.StartupPath + "\\" + tempfile1);
                        var tempfile2 = Path.GetRandomFileName();
                        _svmfile = saveFileDialog2.FileName;
                        var psi = new ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe",
                            "\"" + Application.StartupPath + "\\" + tempfile1 + "\" \"" +
                            Application.StartupPath + "\\" + tempfile2 + "\" " + textBox14.Text)
                        {
                            WorkingDirectory
                                =
                                Application
                                    .StartupPath,
                            UseShellExecute
                                =
                                false
                        };
                        log.Text += "Start learn SVM\r\n";
                        log.Refresh();
                        var process = Process.Start(psi);
                        while (!process.HasExited) Thread.Sleep(1000);
                        log.Text += "Learn SVM complit\r\n";
                        File.Delete(Application.StartupPath + "\\" + tempfile1);
                        if (saveFileDialog3.ShowDialog() == DialogResult.OK)
                        {
                            var f = File.ReadAllLines(Application.StartupPath + "\\" + tempfile2);
                            var i = Convert.ToInt32(f[7].Split(' ')[0]);
                            var j = Convert.ToInt32(f[9].Split(' ')[0]) - 1;
                            var a = Convert.ToDouble(f[10].Split(' ')[0], CultureInfo.InvariantCulture);
                            var ar = new double[j, i];
                            var al = new double[j];
                            for (var index0 = 11; index0 < f.Length; index0++)
                            {
                                var s = f[index0].TrimEnd('#');
                                var m = s.Split(' ');
                                al[index0 - 11] = Convert.ToDouble(m[0], CultureInfo.InvariantCulture);
                                for (var k = 1; k < m.Length - 1; k++)
                                    ar[index0 - 11, Convert.ToInt32(m[k].Split(':')[0]) - 1] =
                                        Convert.ToDouble(m[k].Split(':')[1], CultureInfo.InvariantCulture);

                            }
                            var d = new float[i + 1];
                            var fff = "";
                            for (var index0 = 0; index0 < i; index0++)
                            {
                                d[index0] = 0;
                                for (var tt = 0; tt < j; tt++)
                                    d[index0] += (float)(al[tt] * ar[tt, index0]);
                                fff += d[index0].ToString(CultureInfo.InvariantCulture) + " ";
                            }

                            d[i] = -(float)a;
                            fff += (-a).ToString(CultureInfo.InvariantCulture);
                            File.WriteAllText(saveFileDialog3.FileName, fff);
                            log.Text += "Vector compute and save.\r\n";
                        }
                        File.Delete(tempfile2);
                    }
                }
                else
                {
                    log.Text += "Error! directory " + textBox2.Text + " not found.\r\n";
                }
            }
            else
            {
                log.Text += "Error! directory " + textBox1.Text + " not found.\r\n";
            }


        }

        private void button8_Click(object sender, EventArgs e)
        {
            var h = new TestHOG();
            h.Show();
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                if (Directory.Exists(textBox2.Text))
                {
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK && SetSetting())
                    {
                        log.Text += "\r\n Start create file to FANN (HOGDescriptor)\r\n";
                        log.Refresh();
                        progressBar1.Value = 0;

                        var posFile = Directory.GetFiles(textBox1.Text).ToList();
                        var negFile = Directory.GetFiles(textBox2.Text).ToList();
                        if (checkBox1.Checked)
                        {
                            posFile.AddRange(Directory.GetDirectories(textBox1.Text).SelectMany(Directory.GetFiles));
                            negFile.AddRange(Directory.GetDirectories(textBox2.Text).SelectMany(Directory.GetFiles));
                        }
                        if (_worker != null && _worker.ThreadState == ThreadState.Running)
                            _worker.Abort();
                        _worker = new Thread(() => ComputeHogann(posFile, negFile, saveFileDialog1.FileName));
                        _worker.Start();
                    }

                }
                else
                {
                    log.Text += "Error! directory " + textBox2.Text + " not found.\r\n";
                }
            }
            else
            {
                log.Text += "Error! directory " + textBox1.Text + " not found.\r\n";
            }
        }

        private object loo = new object();

        private void ComputeHogann(List<List<string>> files, string output)
        {

            ProgressVisible(true);
            if (File.Exists(output))
                File.Delete(output);

            var full = files.Sum(t => t.Count);
            var pos = 0;
            var neg = 0;
            var ff = 0;

            var lastrp = 0;
            var l = new List<string>();

            for (int dir = 0; dir < files.Count; dir++)
            {
                string t = "";
                for (int i = 0; i < files.Count; i++)
                    t += i == dir ? "1 " : "0 ";
                t = t.TrimEnd(' ');
                Parallel.ForEach(files[dir], file =>
                // foreach (var file in files[dir])
                {


                    try
                    {
                        var f = Hog(file);
                        if (ff == 0) ff = f.Length;
                        var sb = new StringBuilder();
                        for (var i = 0; i < f.Length; i++)
                            if (i == 0)
                                sb.Append(f[i].ToString(CultureInfo.InvariantCulture));
                            else
                                sb.AppendFormat(" {0}", f[i].ToString(CultureInfo.InvariantCulture));
                        var h = sb.ToString();
                        lock (loo)
                        {
                            if (l.IndexOf(h) < 0)
                            {
                                l.Add(h);
                                l.Add(t);
                                pos++;

                            }

                        }


                    }
                    catch (Exception exception)
                    {
                        SetText(file + "\r\n" + (exception.Message) + "\r\n");
                    }
                    if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp)
                    {
                        lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                        Progress(lastrp);
                    }

                });
            }



            l.Insert(0, neg + pos + " " + ff + " " + files.Count);
            File.WriteAllLines(output, l.ToArray());

            SetText(" Compute HogDescriptors copmlit. count=" + pos + "\r\n");

            _hogfile = output;
            SetText("HogDescriptors file to FANN save to " + output + "\r\n");
            ProgressVisible(false);
        }

        private void ComputeHogann(List<string> posFile, ICollection<string> negFile, string output)
        {

            ProgressVisible(true);
            if (File.Exists(output))
                File.Delete(output);
            var full = posFile.Count + negFile.Count;
            var pos = 0;
            var neg = 0;
            var ff = 0;
            var poss = 0;
            var negs = 0;
            var nege = 0;
            var lastrp = 0;
            var l = new List<string>();
            Parallel.ForEach(posFile, file =>
            //  foreach (var file in posFile)
            {

                try
                {
                    var f = Hog(file);
                    if (ff == 0) ff = f.Length;
                    var sb = new StringBuilder();
                    for (var i = 0; i < f.Length; i++)
                        if (i == 0)
                            sb.Append(f[i].ToString(CultureInfo.InvariantCulture));
                        else
                            sb.AppendFormat(" {0}", f[i].ToString(CultureInfo.InvariantCulture));
                    var h = sb.ToString();
                    lock (o)
                    {
                        l.Add(h);
                        l.Add("1 0");
                        pos++;
                    }


                }
                catch (Exception exception)
                {
                    SetText(file + "\r\n" + (exception.Message) + "\r\n");
                }
                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp)
                {
                    lock (o)
                    {
                        lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                        Progress(lastrp);
                    }
                }

            });

            Parallel.ForEach(negFile, file =>
            // foreach (var file in negFile)
            {
                try
                {
                    var f = Hog(file);
                    var sb = new StringBuilder();
                    for (var i = 0; i < f.Length; i++)
                        if (i == 0)
                            sb.Append(f[i].ToString(CultureInfo.InvariantCulture));
                        else
                            sb.AppendFormat(" {0}", f[i].ToString(CultureInfo.InvariantCulture));
                    var h = sb.ToString();
                    lock (o)
                    {
                        l.Add(h);
                        l.Add("0 1");
                        neg++;
                    }



                }
                catch (Exception exception)
                {
                    SetText(file + "\r\n" + (exception.Message) + "\r\n");

                }
                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp)
                {
                    lock (o)
                    {
                        lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                        Progress(lastrp);
                    }
                }

            });
            l.Insert(0, neg + pos + " " + ff + " 2");
            File.WriteAllLines(output, l.ToArray());

            SetText(" Compute HogDescriptors copmlit. pos=" + pos + " neg=" +
                    neg + "\r\n");
            SetText(" PosClone=" + poss + " NegClone " + negs + " Eror" + nege + "\r\n");
            _hogfile = output;
            SetText("HogDescriptors file to FANN save to " + output + "\r\n");
            ProgressVisible(false);
        }


        private Thread _worker;

        private void button3_Click(object sender, EventArgs e)
        {
            _tstate = true;
            if (Directory.Exists(textBox1.Text))
            {
                if (Directory.Exists(textBox2.Text))
                {
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK && SetSetting())
                    {
                        progressBar1.Visible = true;
                        log.Text += "\r\n Start create file to SVM Light (HOGDescriptor)\r\n";
                        log.Refresh();
                        progressBar1.Value = 0;

                        var posFile = Directory.GetFiles(textBox1.Text).ToList();
                        var negFile = Directory.GetFiles(textBox2.Text).ToList();
                        if (checkBox1.Checked)
                        {
                            posFile.AddRange(Directory.GetDirectories(textBox1.Text).SelectMany(Directory.GetFiles));
                            negFile.AddRange(Directory.GetDirectories(textBox2.Text).SelectMany(Directory.GetFiles));
                        }
                        if (_worker != null && _worker.ThreadState == ThreadState.Running)
                            _worker.Abort();

                        _worker = new Thread(() => ComputeHOG(posFile, negFile, saveFileDialog1.FileName))
                        {
                            Priority = ThreadPriority.Highest
                        };
                        _worker.Start();
                        while (!_tstate)
                            Thread.CurrentThread.Join(1000);
                    }

                }
                else
                {
                    log.Text += "Error! directory " + textBox2.Text + " not found.\r\n";
                }
            }
            else
            {
                log.Text += "Error! directory " + textBox1.Text + " not found.\r\n";
            }
        }

        private string _svmfile;

        private void button4_Click(object sender, EventArgs e)
        {
            if (File.Exists(_hogfile))
            {
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(_hogfile);
                openFileDialog1.FileName = Path.GetFileName(_hogfile);
            }
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                saveFileDialog2.FileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName) + ".svm";
                if (saveFileDialog2.ShowDialog() == DialogResult.OK)
                {
                    _svmfile = saveFileDialog2.FileName;
                    var psi = new ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe",
                        "\"" + openFileDialog1.FileName + "\" \"" + saveFileDialog2.FileName + "\" " + textBox14.Text)
                    {
                        WorkingDirectory = Application.StartupPath,
                        UseShellExecute = false
                    };
                    Process.Start(psi);


                }
            }
        }


        #endregion

        #region haar

        private void CreateSamplesHaar(List<string> posFile, List<string> negFile)
        {
            var
                lastrp = 0;
            if (Directory.Exists("c:\\temp\\haar\\positive"))
                Directory.Delete("c:\\temp\\haar\\positive", true);
            Directory.CreateDirectory("c:\\temp\\haar\\positive");

            if (Directory.Exists("c:\\temp\\haar\\negative"))
                Directory.Delete("c:\\temp\\haar\\negative", true);
            Directory.CreateDirectory("c:\\temp\\haar\\negative");
            ProgressVisible(true);
            if (File.Exists("c:\\temp\\haar\\vec.dat"))
                File.Delete("c:\\temp\\haar\\vec.dat");
            if (File.Exists("c:\\temp\\haar\\bg.dat"))
                File.Delete("c:\\temp\\haar\\bg.dat");
            var full = posFile.Count + negFile.Count;
            var pos = 0;
            var neg = 0;
            var sb = new StringBuilder();
            foreach (var file in posFile)
            {
                try
                {
                    var i = new IplImage(file);
                    sb.AppendFormat("positive\\{0} 1 0 0 {1} {2}", pos + Path.GetExtension(file), i.Width, i.Height);
                    sb.AppendLine();
                    File.Copy(file, "c:\\temp\\haar\\positive\\" + pos + Path.GetExtension(file), true);
                    pos++;
                    i.Dispose();
                }
                catch (Exception exception)
                {
                    SetText(file + "\r\n" + (exception.Message) + "\r\n");

                }
                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp)
                {
                    lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                    Progress(lastrp);
                }
            }

            File.WriteAllText("c:\\temp\\haar\\vec.dat", sb.ToString(), Encoding.GetEncoding(1251));
            sb.Clear();
            var psi = new ProcessStartInfo(Application.StartupPath + "\\createsamples.exe",
                "-info c:\\temp\\haar\\vec.dat -vec c:\\temp\\haar\\vec.vec -w " + _w + " -h " + _h + " -num " + _n +
                " -maxxangle " + _a1 + " -maxyangle " + _a2 + " -maxzangle " + _a3)
            {
                WorkingDirectory = Application.StartupPath,
                UseShellExecute = false
            };
            var process = Process.Start(psi);
            while (!process.HasExited) Thread.Sleep(1000);
            _bg = 0;
            foreach (var file in negFile)
            {
                try
                {
                    var i = new IplImage(file);
                    sb.AppendFormat("negative\\{0}", Path.GetFileName(file));
                    if (i.Width > 640)
                    {
                        var r = new IplImage(640, 480, i.Depth, i.NChannels);
                        Cv.Resize(i, r);
                        r.SaveImage("c:\\temp\\haar\\negative\\" + Path.GetFileName(file));
                        r.Dispose();
                    }
                    else
                        File.Copy(file, "c:\\temp\\haar\\negative\\" + Path.GetFileName(file));

                    sb.AppendLine();
                    neg++;
                    i.Dispose();
                    _bg++;



                }
                catch (Exception ex)
                {
                    SetText(file + "\r\n" + (ex.Message) + "\r\n");




                }
                if (Convert.ToInt32((pos + neg) * 100 / full) > lastrp)
                {
                    lastrp = Convert.ToInt32((pos + neg) * 100 / full);
                    Progress(lastrp);
                }

            }
            File.WriteAllText("c:\\temp\\haar\\bg.dat", sb.ToString(), Encoding.GetEncoding(1251));
            try
            {
                SetText("Samples Created in c:\\temp\\haar\r\n");
                ProgressVisible(false);
            }
            catch
            {
            }


        }



        #endregion

        #region разметка

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(openFileDialog3.FileName, FileMode.Open, FileAccess.ReadWrite);
                _examples = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                ShowEx();
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            ShowEx();
        }

        private double _x, _y;
        private double _xe, _ye;
        private bool _mm;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (_examples != null && listBox1.Items.Count > 0 && listBox1.SelectedIndex >= 0)
            {
                _x = e.X / Convert.ToDouble(pictureBox1.ClientRectangle.Width) * 100 - 50;
                _y = e.Y / Convert.ToDouble(pictureBox1.ClientRectangle.Height) * 100 - 50;
                _mm = true;
                if (_examples[_selS[listBox1.SelectedIndex]].Rect == null)
                    _examples[_selS[listBox1.SelectedIndex]].Rect = new List<RectangleF>();
                _examples[_selS[listBox1.SelectedIndex]].Rect.Add(new RectangleF((float)_x, (float)_y, 0, 0));
                propertyGrid1.SelectedObject = _examples[_selS[listBox1.SelectedIndex]];
                pictureBox1.Image = bitmap(_examples[_selS[listBox1.SelectedIndex]]);
                pictureBox1.Refresh();
            }

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_mm)
            {
                _xe = e.X / Convert.ToDouble(pictureBox1.ClientRectangle.Width) * 100 - 50;
                _ye = e.Y / Convert.ToDouble(pictureBox1.ClientRectangle.Height) * 100 - 50;
                _mm = true;
                _examples[_selS[listBox1.SelectedIndex]].Rect[
                    _examples[_selS[listBox1.SelectedIndex]].Rect.Count - 1] =
                    new RectangleF((float)(_xe > _x ? _x : _xe), (float)(_ye > _y ? _y : _ye),
                        (float)Math.Abs(_x - _xe),
                        (float)Math.Abs(_y - _ye));

                pictureBox1.Image = bitmap(_examples[_selS[listBox1.SelectedIndex]]);
                pictureBox1.Refresh();
                propertyGrid1.SelectedObject = _examples[_selS[listBox1.SelectedIndex]];
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            _mm = false;
            //listBox1.SelectedIndex++;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (_examples != null && listBox1.Items.Count > 0 && listBox1.SelectedIndex >= 0)
            {
                _examples.RemoveAt(_selS[listBox1.SelectedIndex]);
                ShowEx();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            saveFileDialog4.FileName = openFileDialog3.FileName;
            if (saveFileDialog4.ShowDialog() == DialogResult.OK)
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(saveFileDialog4.FileName, FileMode.Create, FileAccess.ReadWrite);
                xml.Serialize(stream, _examples);
                stream.Close();
                ShowEx();
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (_examples != null)
            {
                if (openFileDialog4.ShowDialog() == DialogResult.OK)
                {
                    foreach (var variable in openFileDialog4.FileNames)
                    {
                        var eduExamples = new EduExamples { File = variable };
                        _examples.Add(eduExamples);

                    }
                    ShowEx();
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                }

            }
        }

        private List<int> _selS;
        private List<EduExamples> _examples;

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            _examples = new List<EduExamples>();
            ShowEx();

        }


        private Bitmap bitmap(EduExamples ex)
        {
            var i = new IplImage(ex.File);
            if (ex.Rect != null)
                foreach (var r in ex.Rect)
                    i.DrawRect(Convert.ToInt32((r.X + 50) * 0.01 * i.Width),
                        Convert.ToInt32((r.Y + 50) * 0.01 * i.Height),
                        Convert.ToInt32((r.X + 50 + r.Width) * 0.01 * i.Width),
                        Convert.ToInt32((r.Y + 50 + r.Height) * 0.01 * i.Height), CvColor.Red, 1);

            var stream = new MemoryStream();
            i.ToStream(stream, ".bmp");
            var b = new Bitmap(stream);

            return b;

        }

        private void ShowEx()
        {
            _selS = new List<int>();
            listBox1.Items.Clear();
            if (_examples != null)
            {
                for (var index = 0; index < _examples.Count; index++)
                {
                    var eduExamplese = _examples[index];
                    if (checkBox3.Checked || !(eduExamplese.Rect != null && eduExamplese.Rect.Count > 0))
                    {
                        if (eduExamplese.File != null) listBox1.Items.Add(Path.GetFileName(eduExamplese.File));
                        _selS.Add(index);
                    }
                }
            }
        }

        #endregion




        /*----------------------
         * -fannfile
         * -fannlayers
         * -fannepoch
         * -fannrun
         * ---------------------*/

        private void Form1_Shown(object sender, EventArgs e)
        {
            ann = new Ann();
            var s = Environment.GetCommandLineArgs().ToList();
            var index = s.IndexOf("-fannfile");
            if (index >= 0 && index < s.Count - 1 && File.Exists(s[index + 1]))
            {
                ann.OpenData(s[index + 1]);

                textBox65.Text = ann.DataFileName;
                textBox60.Text = ann.Data.NumInputTrainData + " " +
                                 (ann.Data.NumInputTrainData + ann.Data.NumOutputTrainData) / 2 +
                                 " " +
                                 ann.Data.NumOutputTrainData;
                textBox36.Text = ann.DataFileName;


                log.Text += "examples " + ann.Data.TrainingDataLength + " " + ann.Data.NumInputTrainData + "\\" +
                    ann.Data.NumOutputTrainData + "\r\n";
                textBox60.Text = ann.Data.NumInputTrainData + " " + (ann.Data.NumInputTrainData + ann.Data.NumOutputTrainData) / 2 +
                         " " +
                         ann.Data.NumOutputTrainData;
            }

            index = s.IndexOf("-fannlayers");
            if (index >= 0 && index < s.Count - 1)
                textBox60.Text = s[index + 1];
            index = s.IndexOf("-fannepoch");
            if (index >= 0 && index < s.Count - 1)
                textBox58.Text = s[index + 1];

            index = s.IndexOf("-fannalgoritm");

            if (index >= 0 && index < s.Count - 1)
                comboBox6.SelectedIndex = Convert.ToInt32(s[index + 1]);

            index = s.IndexOf("-fannrun");
            if (index >= 0) StartSimpleTrein(null, null);

        }

        #region HOG разметка

        private bool _hogrun;

        private void openMarkingFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == DialogResult.OK)
                markingFileTextBox.Text = openFileDialog4.FileName;
        }

        private List<EduExamples> _hogex;

        private delegate void SetHogCalback(double n, double v, int index);

        public void SetHog(double n, double v, int index)
        {
            if (hogChart.InvokeRequired && hogChart.InvokeRequired)
            {
                var d = new SetHogCalback(SetHog);
                Invoke(d, new object[] { n, v, index });
            }
            else
            {
                hogChart.Series[0].Points.AddXY(index, n);
                hogChart.Series[1].Points.AddXY(index, v);
            }

        }

        private float angle;


        public CvPoint2D32f HogMStep(List<EduExamples> allExaples, List<EduExamples> newExaples, int iteration)
        {
            Directory.CreateDirectory("texmphog\\neg\\" + iteration);
            var hog = LoadHogFromSVM(Application.StartupPath + "\\texmphog\\dat\\" + (iteration - 1) + ".svm");

            int newnegCount = 0;
            var ran = new List<int>();
            for (var i = 0; i < allExaples.Count; i++)
                ran.Add(i);
            var random = new Random();
            double count = 0;
            double ver = 0;
            double vercount = 0;
            double found = 0;
            for (int i = 0; i < allExaples.Count; i++)
            {
                count++;
                var bb = false;
                var rr = random.Next(0, ran.Count);
                var ex = allExaples[ran[rr]];
                ran.RemoveAt(rr);
                var im = new IplImage(ex.File, LoadMode.GrayScale);

                var r = ex.Rect.Select(
                      t =>
                          new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * im.Width),
                              Convert.ToInt32((t.Y + 50) * 0.01 * im.Height),
                              Convert.ToInt32(t.Width * 0.01 * im.Width),
                              Convert.ToInt32(t.Height * 0.01 * im.Height))).ToList();

                var ttt = hog.DetectMultiScale(new Mat(im));
                foreach (var f2 in ttt)
                {
                    found++;
                    var kw = Convert.ToInt32(textBox31.Text) / 100.0;
                    bool b = r.Any(t => t.Intersect(f2).Width * t.Intersect(f2).Height > t.Height * t.Width * kw);
                    if (!b)
                    {
                        var color = new IplImage(im.Size, im.Depth, 3);
                        Cv.CvtColor(im, color, ColorConversion.GrayToBgr);
                        color.DrawRect(r[0], CvColor.Blue);
                        color.DrawRect(f2, CvColor.Red);
                        color.SaveImage("texmphog\\neg\\" + iteration + "\\1.jpg");
                        color.Dispose();
                        var cut = im.GetSubImage(f2);
                        cut.SaveImage("texmphog\\neg\\" + iteration + "\\" + Guid.NewGuid() + ".bmp");
                        newnegCount++;
                        cut.Dispose();
                    }
                    else
                    {

                        vercount++;
                        bb = true;
                    }
                }
                if (bb)
                    ver++;
                im.Dispose();
                if (newnegCount > 1000)
                    break;
            }

            ran = new List<int>();
            for (var i = 0; i < newExaples.Count; i++)
                ran.Add(i);

            var c = 0;

            for (int i = 0; i < ran.Count; i++)
            {

                var ex = newExaples[random.Next(0, newExaples.Count)];

                var im = new IplImage(ex.File, LoadMode.GrayScale);
                var r =
                    ex.Rect.Select(
                        t =>
                            new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * im.Width),
                                Convert.ToInt32((t.Y + 50) * 0.01 * im.Height),
                                Convert.ToInt32(t.Width * 0.01 * im.Width),
                                Convert.ToInt32(t.Height * 0.01 * im.Height))).ToList();
                foreach (var rrr in r)
                {

                    var cut = im.GetSubImage(rrr);
                    cut.SaveImage("texmphog\\pos\\" + Guid.NewGuid() + ".bmp");
                    c++;
                    cut.Dispose();
                }
                newExaples.RemoveAll(t => t.File == ex.File);
                if (c > newnegCount)
                    break;
            }

            var posFile = Directory.GetFiles("texmphog\\pos\\").ToList();
            var negFile = Directory.GetDirectories("texmphog\\neg").SelectMany(Directory.GetFiles).ToList();


            var tempfile1 = Path.GetRandomFileName();

            ComputeHOG(posFile, negFile, tempfile1);
            var tempfile2 = iteration + ".svm";
            var psi = new ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe",
                textBox14.Text + " \"" + Application.StartupPath + "\\" + tempfile1 + "\" \"" +
                Application.StartupPath + "\\texmphog\\dat\\" + tempfile2 + "\"")
            {
                WorkingDirectory = Application.StartupPath,
                UseShellExecute = false
            };
            var process = Process.Start(psi);
            while (!process.HasExited) Thread.Sleep(1000);
            File.Delete(Application.StartupPath + "\\" + tempfile1);

            return new CvPoint2D32f(ver / count, vercount / found);
        }


        public HOGDescriptor LoadHogFromSVM(string file)
        {

            var f = File.ReadAllLines(file);
            var i = Convert.ToInt32(f[7].Split(' ')[0]);
            var j = Convert.ToInt32(f[9].Split(' ')[0]) - 1;
            var ar = new double[j, i];
            var a = Convert.ToDouble(f[10].Split(' ')[0], CultureInfo.InvariantCulture);
            var al = new double[j];
            for (var y = 11; y < f.Length; y++)
            {
                var s = f[y].TrimEnd('#');
                var m = s.Split(' ');
                al[y - 11] = Convert.ToDouble(m[0], CultureInfo.InvariantCulture);
                for (var k = 1; k < m.Length - 1; k++)
                    ar[y - 11, Convert.ToInt32(m[k].Split(':')[0]) - 1] = Convert.ToDouble(m[k].Split(':')[1],
                        CultureInfo.InvariantCulture);

            }
            var d = new float[i + 1];
            for (var t = 0; t < i; t++)
            {
                d[t] = 0;
                for (var tt = 0; tt < j; tt++)
                    d[t] += (float)(al[tt] * ar[tt, t]);

            }
            d[i] = -(float)a;
            var hog = new HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins, _aperature, _winSigma);
            hog.SetSVMDetector(d);
            return hog;
        }


        public void HogSvmStart()
        {
            if (!_hogrun)
            {
                if (File.Exists(markingFileTextBox.Text) && SetSetting())
                {
                    var xml = new XmlSerializer(typeof(List<EduExamples>));
                    var stream = new FileStream(markingFileTextBox.Text, FileMode.Open, FileAccess.ReadWrite);
                    var allExamles = (List<EduExamples>)xml.Deserialize(stream);
                    stream.Close();
                    allExamles.RemoveAll(t => t.Rect.Count == 0);
                    var newExaples = allExamles.Select(t => t).ToList();

                    if (Directory.Exists("texmphog"))
                        Directory.Delete("texmphog", true);
                    Directory.CreateDirectory("texmphog");
                    Directory.CreateDirectory("texmphog\\pos");
                    Directory.CreateDirectory("texmphog\\neg");
                    Directory.CreateDirectory("texmphog\\neg\\0");
                    Directory.CreateDirectory("texmphog\\dat");
                    int c = 0;
                    var random = new Random();
                    for (int i = 0; i < newExaples.Count; i++)
                    {
                        var rr = random.Next(0, newExaples.Count);
                        var ex = newExaples[rr];

                        var im = new IplImage(ex.File, LoadMode.GrayScale);
                        var r =
                            ex.Rect.Select(
                                t =>
                                    new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * im.Width),
                                        Convert.ToInt32((t.Y + 50) * 0.01 * im.Height),
                                        Convert.ToInt32(t.Width * 0.01 * im.Width),
                                        Convert.ToInt32(t.Height * 0.01 * im.Height))).ToList();
                        foreach (var rrr in r)
                        {

                            var cut = im.GetSubImage(rrr);
                            cut.SaveImage("texmphog\\pos\\" + Guid.NewGuid() + ".bmp");
                            c++;
                            cut.Dispose();
                        }
                        newExaples.RemoveAll(t => t.File == ex.File);
                        if (c > 1000)
                            break;
                    }

                    var ran = new List<int>();
                    for (var i = 0; i < allExamles.Count; i++)

                        ran.Add(i);
                    c = 0;

                    for (int i = 0; i < ran.Count; i++)
                    {
                        var rr = random.Next(0, ran.Count);
                        var ex = allExamles[ran[rr]];
                        ran.RemoveAt(rr);
                        var im = new IplImage(ex.File, LoadMode.GrayScale);
                        var r =
                            ex.Rect.Select(
                                t =>
                                    new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * im.Width),
                                        Convert.ToInt32((t.Y + 50) * 0.01 * im.Height),
                                        Convert.ToInt32(t.Width * 0.01 * im.Width),
                                        Convert.ToInt32(t.Height * 0.01 * im.Height))).ToList();
                        int n = 0;
                        int nn = 0;
                        while (n < 1 && nn < 10)
                        {
                            nn++;
                            try
                            {
                                float kw = ex.Rect[0].Width / ex.Rect[0].Height;
                                float w = random.Next(30) + 10;
                                float h = w / kw;
                                var rrr = new RectangleF(
                                    random.Next(100 - Convert.ToByte(w)) - 50 + Convert.ToByte(w / 2),
                                    random.Next(100 - Convert.ToByte(h)) - 50 + Convert.ToByte(h / 2),
                                    w, w / kw);
                                if (!
                                    ex.Rect.Any(
                                        t => t.IntersectsWith(rrr)))
                                {
                                    var s = im.GetSubImage(new CvRect(Convert.ToInt32((rrr.Left + 50) * 0.01 * im.Width),
                                        Convert.ToInt32((rrr.Top + 50) * 0.01 * im.Height),
                                        Convert.ToInt32(rrr.Width * 0.01 * im.Width),
                                        Convert.ToInt32(rrr.Height * 0.01 * im.Height)));

                                    s.SaveImage("texmphog\\neg\\0\\" + Guid.NewGuid() + ".bmp");
                                    s.Dispose();
                                    c++;
                                }
                            }
                            catch
                            {
                            }

                        }
                        if (c > 1000)
                            break;
                    }

                    var posFile = Directory.GetFiles("texmphog\\pos\\").ToList();
                    var negFile = Directory.GetDirectories("texmphog\\neg").SelectMany(Directory.GetFiles).ToList();


                    var tempfile1 = Path.GetRandomFileName();
                    _iterhog = 0;
                    ComputeHOG(posFile, negFile, tempfile1);
                    var tempfile2 = _iterhog + ".svm";
                    var psi = new ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe",
                        textBox14.Text + " \"" + Application.StartupPath + "\\" + tempfile1 + "\" \"" +
                        Application.StartupPath + "\\texmphog\\dat\\" + tempfile2 + "\"")
                    {
                        WorkingDirectory = Application.StartupPath,
                        UseShellExecute = false
                    };
                    var process = Process.Start(psi);
                    while (!process.HasExited) Thread.Sleep(1000);
                    File.Delete(Application.StartupPath + "\\" + tempfile1);
                    _hogrun = true;
                    while (_hogrun)
                    {
                        _iterhog++;
                        SetSetting();
                        var f =
                            HogMStep(allExamles, newExaples, _iterhog);
                        SetHog(f.X, f.Y, _iterhog);
                        ;
                    }
                }
            }
            else _hogrun = false;
        }


        private void button19_Click(object sender, EventArgs e)
        {
            if (!_hogrun)
            {
                if (File.Exists(markingFileTextBox.Text) && SetSetting())
                {
                    var xml = new XmlSerializer(typeof(List<EduExamples>));
                    var stream = new FileStream(markingFileTextBox.Text, FileMode.Open, FileAccess.ReadWrite);
                    _hogex = (List<EduExamples>)xml.Deserialize(stream);
                    stream.Close();
                    for (int i = _hogex.Count - 1; i > Convert.ToInt32(textBox30.Text); i--)
                        _hogex.RemoveAt(i);
                    if (Directory.Exists("texmphog"))
                        Directory.Delete("texmphog", true);
                    Directory.CreateDirectory("texmphog");
                    Directory.CreateDirectory("texmphog\\pos");
                    Directory.CreateDirectory("texmphog\\neg");
                    Directory.CreateDirectory("texmphog\\neg\\0");

                    angle = 0;

                    Directory.CreateDirectory("texmphog\\dat");
                    var r = new Random();
                    for (var index = 0; index < _hogex.Count; index++)
                    {
                        var ex = _hogex[index];
                        if (File.Exists(ex.File) && ex.Rect != null && ex.Rect.Count > 0)
                        {
                            var i = new IplImage(ex.File, LoadMode.GrayScale);
                            ex.Rect.RemoveAll(t => t.Width == 0 || t.Height == 0);
                            foreach (var t in ex.Rect)
                            {

                                var ss = i.GetSubImage(new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * i.Width),
                                    Convert.ToInt32((t.Y + 50) * 0.01 * i.Height),
                                    Convert.ToInt32(t.Width * 0.01 * i.Width),
                                    Convert.ToInt32(t.Height * 0.01 * i.Height)));
                                ss.SaveImage("texmphog\\pos\\" + Guid.NewGuid() + ".bmp");


                                ss.Dispose();
                            }
                            int n = 0;
                            int nn = 0;
                            while (n < ex.Rect.Count && nn < ex.Rect.Count*100)
                            {
                                nn++;
                                try
                                {
                                    float kw = ex.Rect[0].Width / ex.Rect[0].Height;
                                    float w = r.Next(30) + Convert.ToInt32(textBox21.Text);
                                    float h = w / kw;
                                    var rr = new RectangleF(r.Next(100 - Convert.ToByte(w)) - 50 + Convert.ToByte(w / 2),
                                        r.Next(100 - Convert.ToByte(h)) - 50 + Convert.ToByte(h / 2),
                                        w, w / kw);
                                    if (!
                                        ex.Rect.Any(
                                            t => t.IntersectsWith(rr)))
                                    //t Contains(rr.Left, rr.Top) || t.Contains(rr.Right, rr.Top) ||
                                    //t.Contains(rr.Right, rr.Bottom) || t.Contains(rr.Left, rr.Bottom) ||
                                    //rr.Contains(t.Left, t.Top) || rr.Contains(t.Right, t.Top) ||
                                    //rr.Contains(t.Right, t.Bottom) || rr.Contains(t.Left, t.Bottom)
                                    //))
                                    {
                                        var s = i.GetSubImage(new CvRect(Convert.ToInt32((rr.Left + 50) * 0.01 * i.Width),
                                            Convert.ToInt32((rr.Top + 50) * 0.01 * i.Height),
                                            Convert.ToInt32(rr.Width * 0.01 * i.Width),
                                            Convert.ToInt32(rr.Height * 0.01 * i.Height)));

                                        s.SaveImage("texmphog\\neg\\0\\" + Guid.NewGuid() + ".bmp");
                                        s.Dispose();
                                        n++;
                                    }
                                }
                                catch
                                {
                                }

                            }
                            i.Dispose();
                        }
                    }
                    _hogrun = true;
                    button19.Text = _hogrun ? "Stop" : "Start";
                    _t = new Thread(AutoHogStart);
                    _t.Start();
                }
            }
            else
            {
                _hogrun = false;
                button19.Text = _hogrun ? "Stop" : "Start";

                if (_hogitem.Count > 0 && _t != null)
                {
                    _t.Abort();
                    var f2 = new Form2(_hogitem, new TextBox(), "");
                    f2.ShowDialog();

                }
            }
        }

        private Thread _t;
        private List<Item> _hogitem;
        private int _iterhog = 1;

        private void AutoHogStart()
        {
            _hogitem = new List<Item>();
            var posFile = Directory.GetFiles("texmphog\\pos\\").ToList();
            var posorig = Directory.GetFiles("texmphog\\pos\\").ToList();
            while (_hogrun)
            {
                SetSetting();
                var f =
                    HOGThrenStep(posFile, posorig);
                SetHog(f.X, f.Y, _iterhog);
                _iterhog++;
            }

        }

        private CvPoint2D32f HOGThrenStep(List<string> posFile, List<string> posOrig)
        {

            var negFile = Directory.GetDirectories("texmphog\\neg").SelectMany(Directory.GetFiles).ToList();
            int maxneg = Convert.ToInt32(textBox30.Text);
            var tempfile1 = Path.GetRandomFileName();
            while (negFile.Count > posFile.Count * 1.5)
            {
                foreach (var ff in posFile)
                {
                    var image = new IplImage(ff, LoadMode.GrayScale);

                    Cv.WarpAffine(image, image,
                        Cv.GetRotationMatrix2D(new CvPoint2D32f(image.Width / 2, image.Height / 2), angle,
                            1 + Math.Abs(angle)));
                    image.SaveImage("texmphog\\pos\\" + Guid.NewGuid() + ".bmp");
                    image.Dispose();
                }
                if (angle > 0)
                    angle = -angle;
                else
                    angle = -angle + 0.05f;
                posFile = Directory.GetFiles("texmphog\\pos\\").ToList();
            }
            ComputeHOG(posFile, negFile, tempfile1);
            var tempfile2 = _iterhog + ".svm";
            var psi = new ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe",
                textBox14.Text + " \"" + Application.StartupPath + "\\" + tempfile1 + "\" \"" +
                Application.StartupPath + "\\texmphog\\dat\\" + tempfile2 + "\"")
            {
                WorkingDirectory = Application.StartupPath,
                UseShellExecute = false
            };
            var process = Process.Start(psi);
            while (!process.HasExited) Thread.Sleep(1000);
            File.Delete(Application.StartupPath + "\\" + tempfile1);

            var f = File.ReadAllLines(Application.StartupPath + "\\texmphog\\dat\\" + tempfile2);
            var i = Convert.ToInt32(f[7].Split(' ')[0]);
            var j = Convert.ToInt32(f[9].Split(' ')[0]) - 1;
            var a = Convert.ToDouble(f[10].Split(' ')[0], CultureInfo.InvariantCulture);
            var ar = new double[j, i];
            var al = new double[j];
            for (var y = 11; y < f.Length; y++)
            {
                var s = f[y].TrimEnd('#');
                var m = s.Split(' ');
                al[y - 11] = Convert.ToDouble(m[0], CultureInfo.InvariantCulture);
                for (var k = 1; k < m.Length - 1; k++)
                    ar[y - 11, Convert.ToInt32(m[k].Split(':')[0]) - 1] = Convert.ToDouble(m[k].Split(':')[1],
                        CultureInfo.InvariantCulture);

            }
            var d = new float[i + 1];

            var fff = "";
            for (var t = 0; t < i; t++)
            {
                d[t] = 0;
                for (var tt = 0; tt < j; tt++)
                    d[t] += (float)(al[tt] * ar[tt, t]);
                fff += d[t].ToString(CultureInfo.InvariantCulture) + " ";
            }

            d[i] = -(float)a;
            fff += (-a).ToString(CultureInfo.InvariantCulture);
            File.WriteAllText(
                Application.StartupPath + "\\texmphog\\dat\\" + Path.GetFileNameWithoutExtension(tempfile2) + ".txt",
                fff);

            var count = 0;
            double o = 0;
            double v = 0;
            double vc = 0;
            var hit = 0.0;
            var hog = new HOGDescriptor(_winSize, _blockSize, _blockStride, _cellSize, _nbins, _aperature, _winSigma);
            hog.SetSVMDetector(d);
            Directory.CreateDirectory("texmphog\\neg\\" + _iterhog);
            Progress(0);
            while (o < _hogex.Count / 3 && v < posFile.Count)
            {
                vc = 0;
                v = 0;
                count = 0;
                o = 0;
                int ob = 0;
                Parallel.ForEach(_hogex, ex =>

                    //    foreach (var ex in _hogex)
                {

                    if (ex.Rect != null && ex.Rect.Count > 0)
                        try
                        {
                            var im = new IplImage(ex.File, LoadMode.GrayScale);
                            var r =
                                ex.Rect.Select(
                                    t =>
                                        new CvRect(Convert.ToInt32((t.X + 50) * 0.01 * im.Width),
                                            Convert.ToInt32((t.Y + 50) * 0.01 * im.Height),
                                            Convert.ToInt32(t.Width * 0.01 * im.Width),
                                            Convert.ToInt32(t.Height * 0.01 * im.Height))).ToList();
                            var yy = 0;
                            var ttt = hog.DetectMultiScale(new Mat(im), hit //, _blockSize, _blockStride, 1.05
                                );
                            foreach (var f2 in ttt)

                            //             if (f2.Left < im.Width && f2.Top < im.Height && f2. >= 0 && f2.Top >= 0)
                            {
                                var kw = Convert.ToInt32(textBox31.Text) / 100.0;

                                count++;


                                bool b = r.Any(t => t.Intersect(f2).Width * t.Intersect(f2).Height > t.Height * t.Width * kw);
                                //false;
                                //foreach (var t in r)
                                //{
                                //    if (Math.Abs((t.Left + t.Width / 2) - (f2.Left + f2.Width / 2)) < t.Width * kw && Math.Abs((t.Top + t.Height / 2) - (f2.Top + f2.Height / 2)) < t.Height * kh)
                                //        b = true;
                                //}

                                if (!b)
                                {

                                    if (o < maxneg)
                                    {
                                        var p = im.GetSubImage(f2);
                                        p.SaveImage("texmphog\\neg\\" + _iterhog + "\\" + o + ".bmp");
                                        p.Dispose();
                                        negFile.Add("texmphog\\neg\\" + _iterhog + "\\" + o + ".bmp");
                                    }
                                    o++;
                                }
                                else
                                {
                                    vc++;
                                    yy++;
                                }
                            }
                            if (yy > 0) v++;
                            im.Dispose();

                        }
                        catch (Exception)
                        {
                        }
                    ob++;
                    Progress(Convert.ToInt32(ob * 100.0 / _hogex.Count));
                }


                    );
                if (hit < -0.8) break;
                hit -= 0.1;
            }
            /*  if (count > hogex.Count)
               for (int k = 0; k < lastcount; k++)
               {
                  File.Delete(negFile[0]);
                   negFile.RemoveAt(0);
               }*/
            _hogitem.Add(new Item(_iterhog, (vc / count) * (v / _hogex.Count) * 100,
                Application.StartupPath + "\\texmphog\\dat\\" + Path.GetFileNameWithoutExtension(tempfile2) + ".txt"));


            return new CvPoint2D32f(vc / count * 100, v / _hogex.Count * 100);
        }

        /* private CvPoint2D32f hogThrenStepEmgu(List<string> posFile)
           {
               List<string> negFile = Directory.GetFiles("texmphog\\neg\\").ToList<string>();

               string tempfile1 = Path.GetRandomFileName();
               ComputeHOG(posFile, negFile, tempfile1);
               string tempfile2 = iterhog + ".svm";
               System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + "\\svm_learn.exe", textBox14.Text + " \"" + Application.StartupPath + "\\" + tempfile1 + "\" \"" +
                   Application.StartupPath + "\\texmphog\\dat\\" + tempfile2 + "\"");
               psi.WorkingDirectory = Application.StartupPath;
               psi.UseShellExecute = false;
               System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
               while (!process.HasExited) System.Threading.Thread.Sleep(1000);
               File.Delete(Application.StartupPath + "\\" + tempfile1);

               string[] f = File.ReadAllLines(Application.StartupPath + "\\texmphog\\dat\\" + tempfile2);
               int i = Convert.ToInt32(f[7].Split(' ')[0]);
               int j = Convert.ToInt32(f[9].Split(' ')[0]) - 1;
               double a = Convert.ToDouble(f[10].Split(' ')[0].Replace('.', ','));
               double[,] ar = new double[j, i];
               double[] al = new double[j];
               for (int y = 11; y < f.Length; y++)
               {
                   string s = f[y].TrimEnd('#');
                   string[] m = s.Split(' ');
                   al[y - 11] = Convert.ToDouble(m[0].Replace('.', ','));
                   for (int k = 1; k < m.Length - 1; k++)
                       ar[y - 11, Convert.ToInt32(m[k].Split(':')[0]) - 1] = Convert.ToDouble(m[k].Split(':')[1].Replace('.', ','));

               }
               float[] d = new float[i + 1];

               for (int t = 0; t < i; t++)
               {
                   d[t] = 0;
                   for (int tt = 0; tt < j; tt++)
                       d[t] += (float)(al[tt] * ar[tt, t]);
               }
               d[i] = -(float)a;

               double sum = 0;
               int count = 0;
               int lastcount = negFile.Count;

               double o = 0;
               double v = 0;
               double vc = 0;
               double hit = -0.5;
                         emguhogcpu.SetSVMDetector(d);
               while (count < hogex.Count * 2)
               {

                   v = 0;
                   foreach (var ex in hogex)
                   {
                       try
                       {


                           Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> im = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>(ex.file);
                           Rectangle r = new Rectangle(Convert.ToInt32((ex.x + 50 - ex.width / 2) * 0.01 * im.Width),
                                                 Convert.ToInt32((ex.y + 50 - ex.height / 2) * 0.01 * im.Height),
                                                 Convert.ToInt32(ex.width * 0.01 * im.Width),
                                                 Convert.ToInt32(ex.height * 0.01 * im.Height));
                           int y = 0;
                           foreach (var f2 in emguhogcpu.DetectMultiScale(im, hit
                                , CvSizeToSize(_blockStride), CvSizeToSize(_winSize),1.03,0,true)
                        
                               )

                               if (f2.Left < im.Width && f2.Top < im.Height && f2.Left >= 0 && f2.Top >= 0)
                               {

                                   count++;
                                   double k = Convert.ToInt32(textBox31.Text) / 100.0;
                                   float ff = (Math.Abs(r.Left - f2.Left) + Math.Abs(r.Right - f2.Right) +
                                               Math.Abs(r.Top - f2.Top) +
                                               Math.Abs(r.Bottom - f2.Bottom)) / 4;
                                   //    if (ff > Convert.ToInt32(textBox31.Text))
                                   if (!(r.Left >= f2.Left - k * r.Width && r.Right <= f2.Right + k * r.Width
                                         && r.Top >= f2.Top - k * r.Height && r.Bottom <= f2.Bottom + k * r.Height))
                                   {
                                       Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> p = im.GetSubRect(f2);
                                       p.Save("texmphog\\neg\\" + iterhog + " " + o + ".jpg");
                                       p.Dispose();
                                       negFile.Add("texmphog\\neg\\" + iterhog + " " + o + ".jpg");
                                       o++;
                                   }
                                   else
                                   {
                                       vc++;
                                       y++;
                                   }


                                   sum += ff;
                               }
                           if (y > 0) v++;
                           im.Dispose();

                       }
                       catch { }
                   }
                   hit -= 0.1;
               }
           
               hogitem.Add(new item(iterhog, o / count, Application.StartupPath + "\\texmphog\\dat\\" + tempfile2));


               return new CvPoint2D32f(vc / count * 100, v / hogex.Count * 100);
           }
           */

        #endregion


        private void button21_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK) textBox32.Text = openFileDialog3.FileName;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                textBox33.Text = openFileDialog3.FileName;
                textBox35.Text = File.ReadAllLines(textBox33.Text).Length.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            SetSetting();
            Directory.CreateDirectory("AnnRun");

            if (openFileDialog3.ShowDialog() == DialogResult.OK)
                if (openFileDialog4.ShowDialog() == DialogResult.OK)
                {
                    var net = new NeuralNet();
                    net.CreateFromFile(openFileDialog3.FileName);
                    for (var i = 0; i < net.GetNumOutput(); i++)
                        Directory.CreateDirectory("AnnRun\\" + i);
                    foreach (var variable in openFileDialog4.FileNames)
                    {
                        try
                        {
                            var f = Hog(variable);
                            var i = new double[f.Length];
                            for (var j = 0; j < f.Length; j++)
                                i[j] = f[j];
                            var o = net.Run(i);

                            var index = 0;

                            for (var j = 0; j < o.Length; j++)
                                if (o[index] < o[j]) index = j;
                            if (checkBox7.Checked)
                            {
                                File.Copy(variable, "AnnRun\\" + index + "\\" + Path.GetFileName(variable));
                            }
                            else
                            {
                                if (
                                    o[index] > Convert.ToDouble(textBox62))
                                    File.Copy(variable, "AnnRun\\" + index + "\\" + Path.GetFileName(variable));
                            }
                        }
                        catch { }

                    }


                }

        }

        private void button20_Click_1(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK && SetSetting())
                {
                    List<List<String>> files = new List<List<string>>();
                    log.Text += "\r\n Start create file to FANN (HOGDescriptor)\r\n";
                    log.Refresh();
                    progressBar1.Value = 0;

                    foreach (var dir in Directory.GetDirectories(textBox1.Text))
                        files.Add(Directory.GetFiles(dir).ToList());
                    if (_worker != null && _worker.ThreadState == ThreadState.Running)
                        _worker.Abort();
                    _worker = new Thread(() => ComputeHogann(files, saveFileDialog1.FileName));
                    _worker.Start();
                }


            }
            else
            {
                log.Text += "Error! directory " + textBox2.Text + " not found.\r\n";
            }

        }





        private void button27_Click(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == DialogResult.OK)
                textBox37.Text = openFileDialog4.FileName;
        }

        private void button26_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox37.Text) && SetSetting())
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox37.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();
                double w = Convert.ToInt32(textBox38.Text);
                double h = Convert.ToInt32(textBox39.Text);
                double dx = Convert.ToInt32(textBox41.Text);
                double dy = Convert.ToInt32(textBox41.Text);
                int ff = 0;
                int count = 0;
                System.IO.TextWriter file = File.CreateText("temp.dat");
                for (int index = 0; index < 1000; index++)
                {
                    if (File.Exists(_hogex[index].File) && _hogex[index].Rect.Count > 0)
                    {
                        var ex = _hogex[index];
                        IplImage image = new IplImage(ex.File, LoadMode.GrayScale);
                        double eX = ex.Rect[0].X + 50 + ex.Rect[0].Width / 2;
                        double eY = ex.Rect[0].Y + 50 + ex.Rect[0].Height / 2;
                        for (double x = w / 2; x < 100 - w / 2; x += dx)
                            for (double y = h / 2; y < 100 - h / 2; y += dy)
                            {
                                var im =
                                    image.GetSubImage(new CvRect(Convert.ToInt32(image.Width * (x - w / 2) * 0.01),
                                        Convert.ToInt32(image.Height * (y - h / 2) * 0.01),
                                        Convert.ToInt32(w * 0.01 * image.Width), Convert.ToInt32(h * 0.01 * image.Height)));
                                var f = Hog(im);
                                im.Dispose();
                                ff = f.Length;
                                var sb = new StringBuilder();
                                for (var i = 0; i < f.Length; i++)
                                    if (i == 0)
                                        sb.Append(f[i].ToString(CultureInfo.InvariantCulture));
                                    else
                                        sb.AppendFormat(" {0}", f[i].ToString(CultureInfo.InvariantCulture));
                                file.WriteLine(sb.ToString());
                                file.WriteLine(Math.Abs(x - eX) < dx && Math.Abs(y - eY) < dy ? "1 0" : "0 1");
                                //  l.Add(sb.ToString());
                                //l.Add(Math.Abs(x - eX) < dx && Math.Abs(y - eY) < dy ? "1 0" : "0 1");
                                count++;
                            }
                        image.Dispose();
                    }
                }
                file.Close();
                System.IO.TextReader fileReader = File.OpenText("temp.dat");
                System.IO.TextWriter write = File.CreateText("ex.dat");
                write.WriteLine(count + " " + ff + " 2");
                string line;
                while ((line = fileReader.ReadLine()) != null)
                {
                    write.WriteLine(line);
                }
                fileReader.Close();
                write.Close();
                File.Delete("temp.dat");
                //l.Insert(0, count + " " + ff + " 2");
                //File.WriteAllLines("ex.dat", l.ToArray());

                SetText("HogDescriptors file to FANN save to ex.dat count" + count + "\r\n");




            }
        }

        private void button28_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox37.Text) && SetSetting())
            {

                FANN.Net.NeuralNet net = new NeuralNet();
                net.CreateFromFile(textBox42.Text);
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox37.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();
                double w = Convert.ToInt32(textBox38.Text);
                double h = Convert.ToInt32(textBox39.Text);
                double dx = Convert.ToInt32(textBox41.Text);
                double dy = Convert.ToInt32(textBox41.Text);

                int count = 0;
                List<double> dist = new List<double>();
                for (int index = 0; index < 100; index++)
                {
                    List<List<double>> poit = new List<List<double>>();

                    var ex = _hogex[index];
                    IplImage image = new IplImage(ex.File, LoadMode.GrayScale);
                    double eX = ex.Rect[0].X + 50 + ex.Rect[0].Width / 2;
                    double eY = ex.Rect[0].Y + 50 + ex.Rect[0].Height / 2;
                    for (double x = w / 2; x < 100 - w / 2; x += dx)
                        for (double y = h / 2; y < 100 - h / 2; y += dy)
                        {
                            var im =
                                image.GetSubImage(new CvRect(Convert.ToInt32(image.Width * (x - w / 2) * 0.01),
                                    Convert.ToInt32(image.Width * (y - h / 2) * 0.01), Convert.ToInt32(w), Convert.ToInt32(h)));
                            var f = Hog(im).Select(t => (double)t).ToArray();
                            im.Dispose();
                            var o = net.Run(f);
                            if (o[0] > 0.5)
                                poit.Add(new List<double> { x, y, o[0] });

                        }
                    if (poit.Count > 0)
                    {
                        poit.Sort((p1, p2) => p2[2].CompareTo(p1[2]));
                        dist.Add(Math.Sqrt((eX - poit[0][0]) * (eX - poit[0][0]) + (eY - poit[0][1]) * (eY - poit[0][1])));
                    }
                    image.Dispose();
                    count++;
                }


                chart5.Series[0].Color = Color.Green;

                chart5.Series[0].Points.Clear();

                for (double i = dist.Min(); i <= dist.Max(); i += (dist.Max() - dist.Min()) / 20)
                {
                    double vc = 0;
                    double ec = 0;
                    foreach (var d in dist)
                    {
                        if (d < i) vc++;
                    }

                    chart5.Series[0].Points.AddXY(i, vc / dist.Count);

                }

                SetText("HogDescriptors file to FANN save to ex.dat count" + count + "\r\n");




            }
        }

        private void button29_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
                textBox42.Text = openFileDialog3.FileName;

        }

        private void button32_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox48.Text) && SetSetting())
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox48.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();

                int dx = Convert.ToInt32(textBox45.Text);
                int dy = Convert.ToInt32(textBox44.Text);
                int ff = 0;
                int count = 0;
                System.IO.TextWriter file = File.CreateText("temp.dat");
                for (int index = 0; index < Math.Min(_hogex.Count, Convert.ToInt32(textBox46.Text)); index++)
                {
                    if (File.Exists(_hogex[index].File) && _hogex[index].Rect.Count > 0)
                    {
                        var image = new IplImage(_hogex[index].File, LoadMode.GrayScale);
                        var resizeImage = new IplImage(new CvSize(320, 240), image.Depth, 1);
                        Cv.Resize(image, resizeImage);
                        Stopwatch w = new Stopwatch();

                        var hogs = CalcDensyHog(resizeImage, dx, dy,
                            new CvRect(Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].X + 50) * 0.01)
                                , Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Y + 50) * 0.01),
                                Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].Width) * 0.01),
                                Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Height) * 0.01)));

                        resizeImage.Dispose();
                        image.Dispose();
                        for (int v = 0; v < 2; v++)

                            foreach (var f in hogs[v])
                            {
                                ff = f.Length;
                                var sb = new StringBuilder();
                                for (var i = 0; i < f.Length; i++)
                                    if (i == 0)
                                        sb.Append(f[i].ToString(CultureInfo.InvariantCulture));
                                    else
                                        sb.AppendFormat(" {0}", f[i].ToString(CultureInfo.InvariantCulture));
                                file.WriteLine(sb.ToString());
                                file.WriteLine(v == 0 ? "1 0" : "0 1");

                                count++;
                            }




                    }

                }
                file.Close();
                System.IO.TextReader fileReader = File.OpenText("temp.dat");
                System.IO.TextWriter write = File.CreateText("ex.dat");
                write.WriteLine(count + " " + ff + " 2");
                string line;
                while ((line = fileReader.ReadLine()) != null)
                {
                    write.WriteLine(line);
                }
                fileReader.Close();
                write.Close();
                File.Delete("temp.dat");
                //l.Insert(0, count + " " + ff + " 2");
                //File.WriteAllLines("ex.dat", l.ToArray());

                SetText("HogDescriptors file to FANN save to ex.dat count" + count + "\r\n");



            }




        }

        private List<List<float[]>> CalcDensyHog(IplImage image, int dx, int dy, CvRect rect)
        {
            var ret = new List<List<float[]>>();
            ;
            ret.Add(new List<float[]>());
            ret.Add(new List<float[]>());
            var rdx = Convert.ToInt32(image.Width * dx / 200.0);
            var rdy = Convert.ToInt32(image.Height * dy / 200.0);
            int s = 0;
            for (int x = 0; x < image.Width; x += rdx)
                for (int y = 0; y < image.Height; y += rdy)
                {
                    var r = new CvRect(x, y, Convert.ToInt32(image.Width * dx / 100.0),
                        Convert.ToInt32(image.Width * dx / 100.0));

                    var subImage = image.GetSubImage(r);
                    if (subImage.Width * subImage.Height > 0)
                    {
                        if (r.Intersect(rect).Width > 0)
                        {

                            if (r.Intersect(rect).Width * r.Intersect(rect).Height > r.Height * r.Width * 0.5)
                            {
                                ret[0].Add(Hog(subImage));

                            }
                            rdx = 1;
                            rdy = 1;
                        }
                        else
                        {
                            if (s % 10 == 0)
                            {
                                ret[1].Add(Hog(subImage));
                            }
                            s++;
                            rdx = Convert.ToInt32(image.Width * dx / 200.0);
                            rdy = Convert.ToInt32(image.Height * dy / 200.0);
                        }
                        subImage.Dispose();
                    }
                }
            return ret;

        }


        private List<float[]> CalcDensyHog(IplImage image, int dx, int dy, List<CvPoint> points)
        {
            var ret = new List<float[]>();


            var rdx = Convert.ToInt32(image.Width * dx / 100.0);
            var rdy = Convert.ToInt32(image.Height * dy / 100.0);
            for (int x = 0; x < image.Width; x += rdx)
                for (int y = 0; y < image.Height; y += rdy)
                {
                    var r = new CvRect(x, y, rdx, rdy);
                    var subImage = image.GetSubImage(r);
                    ret.Add(Hog(subImage));
                    points.Add(new CvPoint(x + rdx / 2, y + rdy / 2));
                    subImage.Dispose();
                }
            return ret;

        }


        private void button33_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                textBox48.Text = openFileDialog3.FileName;
            }
        }

        private void button31_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                int dx = Convert.ToInt32(textBox45.Text);
                int dy = Convert.ToInt32(textBox44.Text);
                SetSetting();
                NeuralNet net = new NeuralNet();
                net.CreateFromFile(textBox43.Text);
                var image = new IplImage(openFileDialog3.FileName, LoadMode.GrayScale);
                var resizeImage = new IplImage(new CvSize(320, 240), image.Depth, 1);
                Cv.Resize(image, resizeImage);
                Stopwatch w = new Stopwatch();
                List<CvPoint> point = new List<CvPoint>();
                var hogs = CalcDensyHog(resizeImage, dx, dy, point);
                var colImage = new IplImage(new CvSize(320, 240), image.Depth, 3);
                Cv.CvtColor(resizeImage, colImage, ColorConversion.GrayToBgr);
                for (int i = 0; i < hogs.Count; i++)
                {
                    var o = net.Run(hogs[i].Select(tt => (double)tt).ToArray());
                    colImage.DrawCircle(point[i], 3,
                        new CvColor(Convert.ToInt32(255 * o[1]), Convert.ToInt32(255 * o[0]), 0));
                }

                new CvWindow(openFileDialog3.FileName, WindowMode.NormalGui, colImage);
                resizeImage.Dispose();
                image.Dispose();

            }
        }

        private void button30_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
                textBox43.Text = openFileDialog3.FileName;
        }

        private void button34_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox48.Text) && SetSetting())
            {
                File.Delete("temp.dat");
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox48.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();

                int dx = Convert.ToInt32(textBox45.Text);
                int dy = Convert.ToInt32(textBox44.Text);
                int ff = 0;
                int pos = 0;
                int neg = 0;
                for (int index = 0; index < Math.Min(_hogex.Count, Convert.ToInt32(textBox46.Text)); index++)
                {
                    if (File.Exists(_hogex[index].File) && _hogex[index].Rect.Count > 0)
                    {
                        var image = new IplImage(_hogex[index].File, LoadMode.GrayScale);
                        var resizeImage = new IplImage(new CvSize(320, 240), image.Depth, 1);
                        Cv.Resize(image, resizeImage);
                        Stopwatch w = new Stopwatch();

                        var hogs = CalcDensyHog(resizeImage, dx, dy,
                            new CvRect(Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].X + 50) * 0.01)
                                , Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Y + 50) * 0.01),
                                Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].Width) * 0.01),
                                Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Height) * 0.01)));

                        resizeImage.Dispose();
                        image.Dispose();
                        for (int v = 0; v < 2; v++)
                        {
                            pos += hogs[0].Count;
                            neg += hogs[1].Count;
                            foreach (var f in hogs[v])
                            {
                                ff = f.Length;
                                var sb = new StringBuilder();


                                var j = 1;
                                sb.Append(v == 0 ? "1" : "-1");
                                foreach (var i in f)
                                    sb.AppendFormat(" {0}:{1}", j++, i.ToString(CultureInfo.InvariantCulture));

                                sb.AppendLine();



                                File.AppendAllText("temp.dat", sb.ToString());
                                sb.Clear();





                            }
                        }




                    }

                }

                SetText("HogDescriptors file to FANN save to ex.dat pos=" + pos + " neg=" + neg + "\r\n");



            }

        }

        private void button35_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox48.Text) && SetSetting())
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox48.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();

                var s = File.ReadAllText(textBox43.Text).Split(' ');
                var d = new float[s.Length];
                for (var i = 0; i < s.Length; i++)
                    d[i] = (float)Convert.ToDouble(s[i], CultureInfo.InvariantCulture);

                int dx = Convert.ToInt32(textBox45.Text);
                int dy = Convert.ToInt32(textBox44.Text);
                int ff = 0;
                int count = 0;
                List<Double> neg = new List<double>();
                List<Double> pos = new List<double>();

                for (int index = 0; index < Math.Min(_hogex.Count, Convert.ToInt32(textBox46.Text)); index++)
                {
                    if (File.Exists(_hogex[index].File) && _hogex[index].Rect.Count > 0)
                    {
                        var image = new IplImage(_hogex[index].File, LoadMode.GrayScale);
                        var resizeImage = new IplImage(new CvSize(320, 240), image.Depth, 1);
                        Cv.Resize(image, resizeImage);
                        Stopwatch w = new Stopwatch();

                        var hogs = CalcDensyHog(resizeImage, dx, dy,
                            new CvRect(Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].X + 50) * 0.01)
                                , Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Y + 50) * 0.01),
                                Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].Width) * 0.01),
                                Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Height) * 0.01)));

                        resizeImage.Dispose();
                        image.Dispose();
                        for (int v = 0; v < 2; v++)

                            foreach (var f in hogs[v])
                            {
                                double sum = 0;
                                for (var i = 0; i < f.Length; i++)
                                    sum += f[i] * d[i];
                                sum += d[d.Length - 1];
                                if (v == 0) pos.Add(sum);
                                else neg.Add(sum);
                            }




                    }

                }
                double maxi = -10;
                double maxp = 0;
                double maxn = 0;
                chart6.Series[0].Points.Clear();
                chart6.Series[1].Points.Clear();

                for (double thr = neg.Min(); thr < neg.Max() + 0.1; thr += 0.01)
                {
                    var pe = pos.Count(variable => variable <= thr);
                    var ne = neg.Count(variable => variable > thr);
                    if (maxn * maxp <
                        (((pos.Count - pe) * 100.0) / pos.Count) * (((neg.Count - ne) * 100.0) / neg.Count))
                    {
                        maxp = ((pos.Count - pe) * 100.0) / pos.Count;
                        maxn = ((neg.Count - ne) * 100.0) / neg.Count;

                    }

                    chart6.Series[0].Points.AddXY(thr, ((pos.Count - pe) * 100.0) / pos.Count);
                    chart6.Series[1].Points.AddXY(thr, ((neg.Count - ne) * 100.0) / neg.Count);
                }





            }
        }





        private void button36_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox48.Text) && SetSetting())
            {
                List<double> intersect = new List<double>();
                List<double> array = new List<double>();
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox48.Text, FileMode.Open, FileAccess.ReadWrite);
                _hogex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                List<string> l = new List<string>();
                double th = Convert.ToDouble(textBox49.Text);
                var s = File.ReadAllText(textBox43.Text).Split(' ');
                var d = new float[s.Length];
                for (var i = 0; i < s.Length; i++)
                    d[i] = (float)Convert.ToDouble(s[i], CultureInfo.InvariantCulture);

                int dx = Convert.ToInt32(textBox45.Text);
                int dy = Convert.ToInt32(textBox44.Text);


                for (int index = 0; index < Math.Min(_hogex.Count, Convert.ToInt32(textBox46.Text)); index++)
                {
                    if (File.Exists(_hogex[index].File) && _hogex[index].Rect.Count > 0)
                    {


                        var image = new IplImage(_hogex[index].File, LoadMode.GrayScale);
                        var resizeImage = new IplImage(new CvSize(320, 240), image.Depth, 1);
                        Cv.Resize(image, resizeImage);

                        var or = new CvRect(Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].X + 50) * 0.01)
                            , Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Y + 50) * 0.01),
                            Convert.ToInt32(resizeImage.Width * (_hogex[index].Rect[0].Width) * 0.01),
                            Convert.ToInt32(resizeImage.Height * (_hogex[index].Rect[0].Height) * 0.01));

                        var rdx = Convert.ToInt32(resizeImage.Width * dx / 100.0);
                        var rdy = Convert.ToInt32(resizeImage.Height * dy / 100.0);
                        int minx = resizeImage.Width;
                        int miny = resizeImage.Height;
                        int maxx = 0;
                        int maxy = 0;
                        for (int x = 0; x < resizeImage.Width; x += rdx / 2)
                            for (int y = 0; y < resizeImage.Height; y += rdy / 2)
                            {
                                var r = new CvRect(x, y, rdx, rdy);

                                using (var subImage = resizeImage.GetSubImage(r))
                                {
                                    var h = Hog(subImage);
                                    double sum = 0;
                                    for (var i = 0; i < h.Length; i++)
                                        sum += h[i] * d[i];
                                    sum += d[d.Length - 1];
                                    if (sum > th)
                                    {
                                        if (minx > r.X) minx = r.X;
                                        if (miny > r.Y) miny = r.Y;
                                        if (maxx < r.Right) maxx = r.Right;
                                        if (maxy < r.Bottom) maxy = r.Bottom;
                                    }
                                }
                            }
                        if (minx < maxx && miny < maxy)
                        {
                            var r = new CvRect(minx, miny, maxx - minx, maxy - miny);
                            intersect.Add((r.Intersect(or).Width * r.Intersect(or).Height) / (or.Width * or.Height * 1.0) * 100);
                            array.Add((r.Width * r.Height) / (resizeImage.Width * resizeImage.Height * 1.0) * 100.0);
                        }
                        intersect.Add(0);
                        resizeImage.Dispose();
                        image.Dispose();
                    }
                }
                chart6.Series[0].Points.Clear();
                chart6.Series[1].Points.Clear();

                for (double thr = 1; thr <= 100; thr++)
                {


                    var pe = intersect.Count(t => t >= thr - 1 && t < thr);

                    var ne = array.Count(t => t >= thr - 1 && t < thr);


                    chart6.Series[0].Points.AddXY(thr, (pe * 100.0) / intersect.Count);
                    chart6.Series[1].Points.AddXY(thr, (ne * 100.0) / array.Count);
                }


            }



        }

        private void button39_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox51.Text) && File.Exists(textBox50.Text))
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(textBox51.Text, FileMode.Open, FileAccess.ReadWrite);
                var ex = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                double scale = Convert.ToDouble(textBox52.Text);
                int minN = Convert.ToInt32(textBox53.Text);
                CvSize minSize = new CvSize(Convert.ToInt32(textBox54.Text.Split(';')[0]),
                    Convert.ToInt32(textBox54.Text.Split(';')[1]));
                CvSize maxSize = new CvSize(Convert.ToInt32(textBox55.Text.Split(';')[0]),
                    Convert.ToInt32(textBox55.Text.Split(';')[1]));
                int width = Convert.ToInt32(textBox56.Text);

                List<double> intersect = new List<double>();
                List<double> time = new List<double>();
                Stopwatch w = new Stopwatch();

                using (var haarCascade = new CascadeClassifier(textBox50.Text))

                    for (int index = 0; index < 10000; index++)
                    {
                        var t = ex[index];
                        if (t.Rect.Count > 0)
                        {
                            using (CvMemStorage storage = new CvMemStorage())
                            {
                                using (var image = new IplImage(t.File, LoadMode.GrayScale))
                                using (
                                    var resizeImage = new IplImage(width,
                                        Convert.ToInt32(image.Height / (image.Width / Convert.ToDouble(width))), image.Depth,
                                        1))
                                {
                                    w.Restart();
                                    Cv.Resize(image, resizeImage);
                                    Mat m = new Mat(resizeImage, false);
                                    var faces = haarCascade.DetectMultiScale(m, scale, minN, HaarDetectionType.Zero,
                                        minSize
                                        );

                                    w.Stop();
                                    time.Add(w.ElapsedMilliseconds);
                                    foreach (var r in t.Rect)
                                    {
                                        var or = new CvRect(Convert.ToInt32(resizeImage.Width * (r.X + 50) * 0.01)
                                            , Convert.ToInt32(resizeImage.Height * (r.Y + 50) * 0.01),
                                            Convert.ToInt32(resizeImage.Width * (r.Width) * 0.01),
                                            Convert.ToInt32(resizeImage.Height * (r.Height) * 0.01));

                                        double min = 0;
                                        foreach (var f in faces)
                                        {
                                            var ir = f.Intersect(or);
                                            if ((ir.Width * ir.Height * 100.0) / (or.Width * or.Height) > min)
                                                min = (ir.Width * ir.Height * 100.0) / (or.Width * or.Height);
                                        }
                                        intersect.Add(min);
                                    }
                                }
                            }
                        }
                    }
                chart9.Series[0].Points.Clear();


                for (double thr = 1; thr <= 100; thr++)
                {


                    var pe = intersect.Count(t => t > thr - 1 && t <= thr);




                    chart9.Series[0].Points.AddXY(thr, (pe * 100.0) / intersect.Count);

                }
                SetText("NotFound=" + intersect.Count(t => t < 1) * 100.0 / intersect.Count() + "\r\n");
                SetText("Avgfound=" + intersect.Where(t => t > 0).Average() + "\r\n");
                SetText("AvgTime=" + time.Average() + "\r\n");



            }
        }

        private void button38_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
                textBox51.Text = openFileDialog3.FileName;
        }

        private void button37_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
                textBox50.Text = openFileDialog3.FileName;
        }

        private void button40_Click(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == DialogResult.OK)
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var fan = File.ReadAllLines(openFileDialog4.FileName);
                    if (File.Exists(saveFileDialog1.FileName))
                        File.Delete(saveFileDialog1.FileName);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i < fan.Length; i += 2)
                    {
                        int z = 0;
                        var o = fan[i + 1].TrimEnd(' ').Split(' ');
                        for (int j = 0; j < o.Length; j++)
                            if (o[j] == "1")
                                z = j;
                        if (checkBox6.Checked)
                            sb.Append(z == 0 ? "1" : "-1");
                        else
                            sb.Append(z);
                        var inp = fan[i].TrimEnd(' ').Split(' ');
                        for (var j = 0; j < inp.Length; j++)
                            sb.AppendFormat(" {0}:{1}", j + 1, inp[j]);

                        sb.AppendLine();

                        if (sb.Length > 1000000)
                        {
                            File.AppendAllText(saveFileDialog1.FileName, sb.ToString());
                            sb.Clear();
                        }
                    }

                    File.AppendAllText(saveFileDialog1.FileName, sb.ToString());
                }
        }


        object o = new object();



        private void button45_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new Thread(HogSvmStart);
            t.Start();
        }

        private void button46_Click(object sender, EventArgs e)
        {
            if (File.Exists(markingFileTextBox.Text) && SetSetting())
            {
                var xml = new XmlSerializer(typeof(List<EduExamples>));
                var stream = new FileStream(markingFileTextBox.Text, FileMode.Open, FileAccess.ReadWrite);
                var allExamles = (List<EduExamples>)xml.Deserialize(stream);
                stream.Close();
                var newExaples = allExamles.Select(t => t).ToList();

                SetSetting();
                var f =
                    HogMStep(allExamles, newExaples, 21);
                SetHog(f.X, f.Y, _iterhog);

            }

        }












        #region fann
        private void StartStepTrein(object sender, EventArgs e)
        {
            ann.NewStepSimpleTrain += SetNew;
            ann.NewStepStepTrain += SetStep;
            var laers = textBox60.Text.Split(' ').Select(t => Convert.ToUInt32(t)).ToArray();

            var fh = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox5.Text, true);
            var fo = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox4.Text, true);
            var m = (TrainingAlgorithm)Enum.Parse(typeof(TrainingAlgorithm), comboBox6.Text, true);

            ann.CreateNet(laers, fh, fo, m);

            ann.StartStepTrein(textBox65.Text, Convert.ToInt32(textBox64.Text), Convert.ToInt32(textBox61.Text), Convert.ToInt32(textBox63.Text), Convert.ToInt32(textBox58.Text));
        }
        private void button44_Click_1(object sender, EventArgs e)
        {
            ann.StopSimple();
        }
        private void StartSimpleTrein(object sender, EventArgs e)
        {
            ann.NewStepSimpleTrain += SetNew;
            ann.EndTrain += ShowSaveNew;
            var laers = textBox60.Text.Split(' ').Select(t => Convert.ToUInt32(t)).ToArray();

            var fh = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox5.Text, true);
            var fo = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox4.Text, true);
            var m = (TrainingAlgorithm)Enum.Parse(typeof(TrainingAlgorithm), comboBox6.Text, true);

            ann.CreateNet(laers, fh, fo, m);
            if (checkBox8.Checked)
            {
                var f =
                    Directory.GetFiles(Path.GetDirectoryName(  textBox65.Text) + "\\temp");
                if (f.Length > 0)
                {
                    var ff = f
                        .Where(
                            t =>
                                Path.GetFileNameWithoutExtension(t).Split(' ')[0] ==
                                Path.GetFileNameWithoutExtension(textBox65.Text)).ToList();
                    if (ff.Count > 0)
                    {
                        ann.CreateNet(ff[0]);
                        SetText("load net " + ff[0]);
                    }
                }
            }

            ann.StartSimpleTrein(Convert.ToInt32(textBox58.Text), Convert.ToInt32(textBox59.Text));
        }


        private Ann ann = new Ann();
        private void button49_Click(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                if (checkBox9.Checked)
                {
                    if (ann.OpenData(openFileDialog4.FileName))
                    {
                        textBox65.Text = ann.DataFileName;
                        textBox60.Text = ann.Data.NumInputTrainData + " " +
                                         (ann.Data.NumInputTrainData + ann.Data.NumOutputTrainData) / 2 +
                                         " " +
                                         ann.Data.NumOutputTrainData;
                        textBox36.Text = ann.DataFileName;
                    }
                }
                else textBox65.Text = openFileDialog4.FileName;

        }


        private void Test(object sender, EventArgs e)
        {
            ann.CreateNet(textBox57.Text);
            var test = ann.Test(textBox36.Text);

            chart11.Series[0].Color = Color.Green;
            chart11.Series[1].Color = Color.Red;
            chart11.Series[0].Points.Clear();
            chart11.Series[1].Points.Clear();
            chart14.Series[0].Points.Clear();
            var count = test.Sum(t => t.Sum(tt => tt.Count));
            for (double i = 0; i <= 1; i += 0.05)
            {
                double vc = test.Sum(t => t[0].Count(tt => tt >= i));
                double ec = test.Sum(t => t[1].Count(tt => tt >= i));


                chart11.Series[0].Points.AddXY(i, vc / count);
                chart11.Series[1].Points.AddXY(i, ec / count);
            }
            chart14.Series[0].Points.Clear();
            chart15.Series[0].Points.Clear();
            chart13.Series.Clear();

            for (int index = 0; index < test.Count; index++)
            {
                chart13.Series.Add(index.ToString());
                chart13.Series[index].ChartType =
                    System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;

                var v = test[index];
                chart15.Series[0].Points.AddY(v[0].Count + v[1].Count);
                chart14.Series[0].Points.AddY(Convert.ToDouble(v[0].Count + v[1].Count) / v[0].Count);
                //double[] ee = new double[test.Count];
                for (int i = 0; i < test.Count; i++)
                {
                    //ee[i] = v[2].Count(t => t == i);
                    chart13.Series[index].Points.AddY(v[2].Count(t => t == i));
                }
                //chart13.Series[0].Points. AddXY(index, ee);
            }
        }
        private void button42_Click_1(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox57.Text = openFileDialog4.FileName;
            }
        }



        private void button25_Click_2(object sender, EventArgs e)
        {
            if (openFileDialog4.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox36.Text = openFileDialog4.FileName;
            }
        }


        private void StarctCrossTrein(object sender, EventArgs e)
        {
            ann.NewStepSimpleTrain += SetNew;
            ann.OpenValidateData(textBox36.Text);
            ann.EndTrain += ShowSaveNew;
            var laers = textBox60.Text.Split(' ').Select(t => Convert.ToUInt32(t)).ToArray();

            var fh = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox5.Text, true);
            var fo = (ActivationFunction)Enum.Parse(typeof(ActivationFunction), comboBox4.Text, true);
            var m = (TrainingAlgorithm)Enum.Parse(typeof(TrainingAlgorithm), comboBox6.Text, true);

            ann.CreateNet(laers, fh, fo, m);

            ann.StartValidateTrein(Convert.ToInt32(textBox58.Text), Convert.ToInt32(textBox59.Text));
        }



        #endregion

        private void trackBar5_Scroll(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            chart12.ChartAreas[0].AxisY.Minimum =Math.Min(chart12.Series.Min(t=>t.Points.Min(tt=>tt.YValues.Min())), trackBar1.Value/100.0 );
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {

        }

    }

}

